package org.lyq.erp.config.exception.errorbasics;

import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import org.lyq.erp.config.readproperties.ReadProperties;

public class ErrorMessageBasics implements ErrorMessage {

	public Integer errorMessageNumber = 0;
	
	public String  popuperrorMessage="false";

	public Map<String, String> errorMessageMap;

	public ErrorMessageBasics() {
		instant();
	}

	public Map<String, String> instant() {

		if (errorMessageMap == null)

			return errorMessageMap = new ConcurrentHashMap<String, String>();
		else
			return errorMessageMap;
	}

	public void setMap(Map<String, String> errorMessageMap) {

		this.errorMessageMap = errorMessageMap;
	}

	public Map<String, String> getMap() {
		return errorMessageMap;
	}

	public Integer getErrorMessageNumber() {
		return errorMessageNumber;
	}

	public void setErrorMessageNumber(Integer errorMessageNumber) {
		this.errorMessageNumber = errorMessageNumber;
	}

	@Override
	public void addErrorMessage(String javaBeanField, String code) {
		this.errorMessageNumber++;
		errorMessageMap.put(javaBeanField, getMessage(code));
	}

	@Override
	public void addErrorMessage(String javaBeanField, String code, String explain) {
		this.errorMessageNumber++;
		errorMessageMap.put(javaBeanField, getMessage(code));
	}

	@Override
	public void addErrorMessage(String code) {
		this.errorMessageNumber++;
		errorMessageMap.put(code, getMessage(code));
	}

	private String getMessage(String code) {

		if (code == null)
			return "";

		Properties properties = ReadProperties.getInstance().getErrorMessageProperties();

		return (properties == null || properties.getProperty(code) == null) ? code : properties.getProperty(code);
	}
}
